<?php
/** Zend_Controller_Action */ 
require_once ROOT_PATH . '/library/Soooner/Controller/Action.php';
class MmController extends Soooner_Controller_Action
{
	private $_modelMm;
	private $_modelModel;
	private $_modelModelfield;
	private $_modelDictionaryitems;
    public function init()
    {
        /* Initialize action controller here */
    	$this->initDzTemplate();
    	$this->_modelMm = new Model_Tables_Mm();
    	$this->_modelModel = new Model_Tables_Model();
    	$this->_modelModelfield = new Model_Tables_Modelfield();
    	$this->_modelDictionaryitems = new Model_Tables_Dictionaryitems();
    }
    public function indexAction(){
    	$m = $this->_getParam('m');//模型表名
    	$minfo = $this->_modelModel->getInfo($m);//表信息
    	include $this->_dzTemplate->getFile( 'mm.manage.html' );
    }
    public function indexajaxAction()
    {
    	$m = $this->_getParam('m');//模型表名
    	$minfo = $this->_modelModel->getInfo($m);//表信息
    	$infos0 = $this->_modelModelfield->getList(1,100,"",$m);
    	$options = $GLOBALS['application']->getOptions();
    	$numPerPage = $options["pagecount"];//每页显示的条数
    	$page = intval($this->_getParam('page'));//默认页码
    	$page = empty($page)?'1':$page;
    	$s=$this->_getParam('s');
    	$ItemCount =  $this->_modelMm->getCount($minfo, $infos0, $s);    	
    	$PageCount = ceil($ItemCount/$numPerPage);
    	if($page>$PageCount)$page=$PageCount;
    	$infos = $this->_modelMm->getList( $minfo, $infos0, $page, $numPerPage, $s );
    	include $this->_dzTemplate->getFile( 'ajax/mm.manage.ajax.html' );
    }
    public function addAction(){
    	$m = $this->_getParam('m');//模型表名
    	$minfo = $this->_modelModel->getInfo($m);//表信息
    	$infos0 = $this->_modelModelfield->getList(1,100,"",$m);
    	foreach($infos0 as $item){
    		$item["arr"] = $this->parsestr($item["defaultvalue"]);
    		$infos[] = $item; 
    	}//数组获取
    	$request = $this->getRequest();
    	if ($request->isPost())
    	{
    		$params =$request->getPost();
    		$this->_modelMm->add($params, $minfo);
    		$this->_redirect("/mm/index/m/$m");
    	}
    	include $this->_dzTemplate->getFile( 'mm.add.html' );
    	//print_r($info);
    }
    public function modifyAction()
    {
    	$m = $this->_getParam('m');//模型表名
    	$id = $this->_getParam('id');//编号
    	$minfo = $this->_modelModel->getInfo($m);//表信息
    	$info = $this->_modelMm->getInfo($id,$minfo);//数据信息
    	$infos0 = $this->_modelModelfield->getList(1,100,"",$m);//字段信息    	
    	foreach($infos0 as $item){
    		$item["v"] = $info[$item["field"]];
    		$item["arr"] = $this->parsestr($item["defaultvalue"]);
    		$infos[] = $item;
    	}//数组获取
    	$request = $this->getRequest();
    	if ($request->isPost())
    	{
    		$params =$request->getPost();    	
    		$this->_modelMm->modify($params, $minfo);
    		$this->_redirect("/mm/index/m/$m");
    	}
    	include $this->_dzTemplate->getFile( 'mm.edit.html' );
    }
    public function deleteAction()
    {
    	$m = $this->_getParam('m');//模型表名
    	$id = $this->_getParam('id');//编号
    	$minfo = $this->_modelModel->getInfo($m);//表信息
    	$this->_modelMm->del($minfo,$id);
    	$this->_redirect("/mm/index/m/$m");
    }
    /**
     * 字符串转数组
     * */
    public function parsestr($str){
    	//dict:10
    	$arr = array();
    	if(substr($str, 0, 4)=="dict"){
    		$dictid = substr($str, 5);
    		$arr = $this->_modelDictionaryitems->getDictionary($dictid);
    	}else if(substr($str, 0, 5)=="array"){
    		$str = str_replace("\\","", $str);
    		eval("\$arr =".$str.";");
    	}
    	return $arr;
    }
}
